package com.springboot.controller;

import com.springboot.model.AssistantListFormKey;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorViewNewAssistantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

/**
 * 本控制台用于医生查看不属于自己的助理列表
 */
@Controller
public class DoctorViewNewAssistantController {
    @Autowired
    DoctorViewNewAssistantService doctorViewNewAssistantService;
    /**
     * 医生查看不属于自己的助理列表
     */
    @CrossOrigin
    @RequestMapping(value = "/getUnresAssistantServlet")
    public @ResponseBody
    String doctorViewNewAssistant(@RequestParam("doctorID") String doctorTel) {
        System.out.println("医生查看不属于自己的助理列表");
        try {
            UserLoginForm doctor=doctorViewNewAssistantService.findUserByTel(doctorTel);
            int doctorId=doctor.getUserId();
            ArrayList<AssistantListFormKey> assistantListFormKeys=doctorViewNewAssistantService.findAssistantByDoctor(doctorId);
            //存储该医生已有的助理id
            int []assistantId=new int[assistantListFormKeys.size()];
            for(int i=0;i<assistantListFormKeys.size();i++)
            {
                AssistantListFormKey assistantListFormKey=assistantListFormKeys.get(i);
                assistantId[i]=assistantListFormKey.getAssistantId();
            }
            ArrayList<UserLoginForm> userLoginForms=doctorViewNewAssistantService.findAssistantByType(3);
            //存储用户表中所有的助理id
            int []allAssistantId=new int[userLoginForms.size()];
            for(int i=0;i<userLoginForms.size();i++)
            {
                UserLoginForm userLoginForm=userLoginForms.get(i);
                allAssistantId[i]=userLoginForm.getUserId();
            }
            int []count=new int[userLoginForms.size()];
            for(int i=0;i<userLoginForms.size();i++)
            {
                for(int j=0;j<assistantListFormKeys.size();j++)
                {
                    if(allAssistantId[i]==assistantId[j])
                    {
                        count[i]++;
                    }
                }
            }
            int []newAssistantId=new int[userLoginForms.size()];
            int patientNum=0;
            for(int i=0;i<userLoginForms.size();i++)
            {
                if(count[i]==0)
                {
                    newAssistantId[patientNum]=allAssistantId[i];
                    patientNum++;
                }
            }
            if(patientNum==0)
            {
                return "空";
            }
            else
            {
                for(int i=0;i<patientNum;i++)
                {
                    System.out.println("newAssistantId："+newAssistantId[i]);
                }
                ArrayList<UserLoginForm> assistantList=new ArrayList<>();
                for(int i=0;i<patientNum;i++)
                {
                    UserLoginForm assistant=doctorViewNewAssistantService.findAssistantById(newAssistantId[i]);
                    assistantList.add(assistant);
                }
                System.out.println(assistantList.toString());
                return assistantList.toString();
            }
        } catch (Exception e) {
            return "False";
        }
    }
}
